<?php
class Application_Model_Noticia extends Zend_Db_Table_Abstract{
    protected $_name = 'noticias';

    public function getById($id){
        $select = $this->select()
            ->where("status = 'A' AND id = ?", $id);

        $result = $this->fetchAll($select);
        $arrNoticia = $result->toArray();

        return $arrNoticia;
    }

    public function getLast20(){
        $select = $this->select()
            ->where("status = 'A'")
            ->order(array('fecha DESC', 'id DESC'))
            ->limit(15);

        $result = $this->fetchAll($select);
        $arrNoticias = $result->toArray();
        $allNoticias = array();

        foreach ($arrNoticias as $n){
            $n['Galeria'] = array();
	    	$galeria = array();

            if($n['galeria_id']){
                $obj_galeria = new Application_Model_Galeria();
                $galeria = $obj_galeria->getById($n['galeria_id']);
            }

            array_push($n['Galeria'], $galeria);

            $n['Video'] = array();
	    	$video = array();

            if($n['video_id']){
                $obj_video = new Application_Model_Video();
                $video = $obj_video->getById($n['video_id']);
            }

            array_push($n['Video'], $video);
            array_push($allNoticias, $n);
        }

        return $allNoticias;
    }
	
	public function getLastLimit($limit = 5){
        $select = $this->select()
            ->where("status = 'A'")
            ->order(array('fecha DESC', 'id DESC'))
            ->limit($limit);

        $result = $this->fetchAll($select);
        $arrNoticias = $result->toArray();

        return $arrNoticias;
    }

    public function getAll(){
        $select = $this->select()
            ->where("status = 'A'")
            ->order(array('fecha DESC', 'id DESC'));

        $result = $this->fetchAll($select);
        $arrNoticias = $result->toArray();

        return $arrNoticias;
    }

    public function uploadFoto($file, $dir = null){
        $server_dir = $_SERVER["DOCUMENT_ROOT"];
        $foto_dir = $server_dir."/".$dir."/";

        if(file_exists($foto_dir) && is_writable($foto_dir)){
            if( ( ($file["type"] == "image/gif")
            || ($file["type"] == "image/jpeg")
            || ($file["type"] == "image/pjpeg")
            || ($file["type"] == "image/png") )
            && ($file["size"] < 2000000) ){
                $destination = $foto_dir.$file["name"];

                if(move_uploaded_file($file["tmp_name"], $destination)){
                    return $file["name"];
                }else{
                    error_log("[FOTO_ERROR]".$file['error']." ".$file["name"]);
                    return 0;
                }
            }else{
                error_log("[FOTO_ERROR]no es imagen valida");
                return false;
            }
        }else{
            error_log("[FOTO_ERROR] dir not writable or does not exist");
            return false;
        }
    }

    public function addNoticia($titulo, $foto, $galeria_id, $video_id, $foto_slider, $descripcion, $texto, $fecha, $status){
        $row = $this->createRow();

        $row->titulo = $titulo;
        $row->foto = $foto;
        $row->galeria_id = $galeria_id;
        $row->video_id = $video_id;
        $row->foto_slider = $foto_slider;
        $row->descripcion = $descripcion;
        $row->texto = $texto;
        $row->fecha = $fecha;
        $row->status = $status;

        $row->save();

        $id = $this->_db->lastInsertId();

        return $id;
    }

    public function updateNoticia($titulo, $foto, $galeria_id, $video_id, $foto_slider, $descripcion, $texto, $fecha, $status, $id){

        $row = $this->find($id)->current();

        $row->titulo = $titulo;
        if($foto){
            $row->foto = $foto;
        }
        $row->galeria_id = $galeria_id;
        $row->video_id = $video_id;
        if($foto_slider){
            $row->foto_slider = $foto_slider;
        }
        $row->descripcion = $descripcion;
        $row->texto = $texto;
        $row->fecha = $fecha;
        $row->status = $status;

        if($row->save()){
            return true;
        }else{
            return false;
        }
    }
}
